package com.open.server.api.system;


import com.open.common.annotation.MethodPermission;
import com.open.base.model.Role;
import com.open.system.dispatch.IRoleDispatch;
import com.open.common.model.Page;
import com.open.common.model.PageRequest;
import com.open.common.model.Result;
import com.open.system.model.dto.RoleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色信息
 *
 * @author 大树03
 * @date 2025-02-01 22:55
 */
@RestController
@RequestMapping("/system/role")
public class RoleApi {

    @Autowired
    private IRoleDispatch roleDispatch;

    /**
     * 对象添加
     *
     * @param model 添加的对象
     * @return
     */
    @MethodPermission("role_add")
    @PostMapping("save")
    public Result<Role> save(@RequestBody Role model)  {
        long now = System.currentTimeMillis();
        Result<Role> result = new Result<>();
        roleDispatch.save(model);
        result.setData(model);
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 对象修改
     *
     * @param model 修改的对象
     * @return
     */
    @MethodPermission("role_update")
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody Role model)  {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(roleDispatch.update(model));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据对象列表查询
     *
     * @param model 查询条件
     * @return
     */
    @PostMapping("findByModel")
    public Result<List<Role>> findByModel(@RequestBody Role model)  {
        long now = System.currentTimeMillis();
        Result<List<Role>> result = new Result<>();
        result.setData(roleDispatch.findByModel(model));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据主键查询对象
     *
     * @param id 查询的对象主键
     * @return
     */
    @GetMapping("findByPk")
    public Result<Role> findByPk(@RequestParam("id") Long id)  {
        long now = System.currentTimeMillis();
        Result<Role> result = new Result<>();
        result.setData(roleDispatch.findByPk(id));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 分页查询
     *
     * @param pageRequest 分页查询条件
     * @return
     */
    @MethodPermission("role_query")
    @PostMapping("findPage")
    public Result<Page<Role>> findPage(@RequestBody PageRequest<Role> pageRequest)  {
        long now = System.currentTimeMillis();
        Result<Page<Role>> result = new Result<>();
        result.setData(roleDispatch.findPage(pageRequest.getPage(), pageRequest.getModel()));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据主键删除对象
     *
     * @param ids 删除的对象主键
     * @return
     */
    @MethodPermission("role_delete")
    @GetMapping("remove")
    public Result<Boolean> remove(@RequestParam("ids") List<Long> ids)  {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(roleDispatch.remove(ids));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 获取全部角色和当前账号拥有的角色
     */
    @GetMapping("getRoles/{id}")
    public Result<Map<String, List<Role>>> getRoleMap(@PathVariable Long id) {
        Result<Map<String, List<Role>>> result = new Result<>();
        long now = System.currentTimeMillis();
        result.setData(roleDispatch.getRoleMap(id));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 分配角色
     * @param roleDto
     * @return
     */
    @MethodPermission("role_update")
    @PostMapping("/doAssignRole")
    public Result<Boolean> doAssignRole(@RequestBody RoleDto roleDto) {
        Result<Boolean> result = new Result<>();
        long now = System.currentTimeMillis();
        result.setData(roleDispatch.doAssignRole(roleDto));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 切换角色
     * @param roleId
     * @return
     */
    @GetMapping("/changeRole")
    public Result<Boolean> changeRole(@RequestParam Long roleId) {
        Result<Boolean> result = new Result<>();
        long now = System.currentTimeMillis();
        result.setData(roleDispatch.changeRole(roleId));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }
}